package com.yonyou.pmclouds.messagenotice.constructor.impl;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.repeatwork.entity.RepeatWorkVO;

import java.util.ArrayList;
import java.util.List;

public class RepeatWorkMessageConstructor extends AbstractMessageNoticeConstructor<RepeatWorkVO> {

    private final ProjectSimpleQuery projectSimpleQuery;

    public RepeatWorkMessageConstructor(ProjectSimpleQuery projectSimpleQuery) {
        this.projectSimpleQuery = projectSimpleQuery;
    }

    @Override
    protected MessageNoticeVO[] generateContent(RepeatWorkVO vo) throws BusinessException {
        if (vo == null)
            return new MessageNoticeVO[0];

        ProjectVO projectVO = projectSimpleQuery.queryProject(vo.getPkProject());

        List<MessageNoticeVO> noticeVOList = new ArrayList<>();

        String prefix = "";

        //通知内容
        if(RepeatWorkVO.OPERATION_PASS.equalsIgnoreCase((vo.getOperation()))) {
            prefix = APPROVAL_SUFFIX_ADOPT_MESSAGE;
        }
        if(RepeatWorkVO.OPERATION_RECTIFY.equalsIgnoreCase((vo.getOperation()))) {
            prefix = APPROVAL_SUFFIX_REJECT_MESSAGE;
        }

        String messageContent = APPROVAL_PREFIX_MESSAGE + "[" +
                projectVO.getSname()
                + "]的复工单，" + prefix;
        //给制单人发送消息
        noticeVOList.add(getNoticeVO(vo, projectVO.getSname(), messageContent, vo.getCreator()));


        // 给抄送人发送通知

        return new MessageNoticeVO[0];
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.REPEATWORK_BILLTYPE;
    }

    /**
     * 生成一条消息通知
     *
     * @param vo
     * @param title
     * @param content
     * @param receiver
     * @return
     */
    private MessageNoticeVO getNoticeVO(RepeatWorkVO vo, String title, String content, String receiver) {
        MessageNoticeVO noticeVO = new MessageNoticeVO();
        noticeVO.setRelateType(vo.getBillType());
        noticeVO.setRelateKey(vo.getPkRepeatwork());
        noticeVO.setMessageTitle(title);
        noticeVO.setMessageReceiver(receiver);
        noticeVO.setPkTenant(vo.getPkTenant());
        noticeVO.setMessageContent(content);

        return noticeVO;
    }
}
